package com.odata.springboot.starter.odata.odata.sql.process.impl;

import com.odata.springboot.starter.odata.odata.config.OdataCachedData;
import com.odata.springboot.starter.odata.odata.sql.process.SqlProcess;
import com.odata.springboot.starter.odata.odata.sql.process.SqlProcessChain;
import com.odata.springboot.starter.odata.odata.sql.sqlBuild.OdataClassMetaData;
import com.odata.springboot.starter.odata.odata.sql.sqlBuild.OdataSqlInformation;
import com.odata.springboot.starter.odata.odata.sql.sqlBuild.SqlSyntaxConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.olingo.server.api.OData;
import org.apache.olingo.server.api.uri.UriInfo;
import org.apache.olingo.server.api.uri.queryoption.OrderByItem;
import org.apache.olingo.server.api.uri.queryoption.OrderByOption;
import org.apache.olingo.server.api.uri.queryoption.expression.Expression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * orderby的处理
 * @author chenYX
 * @date 2021/6/28 11:24
 * @description
 */
@Component
@Slf4j
public class OrderByProcessImpl extends SqlProcess {
    @Autowired
    private SqlProcessChain sqlProcessChain;

    @Override
    public void process(OdataSqlInformation.SqlInformationBuild sqlBuild, UriInfo uriInfo, Class<?> aClass, OData oData) {
        OrderByOption orderByOption = uriInfo.getOrderByOption();
        if (orderByOption != null) {
            List<OrderByItem> orders = orderByOption.getOrders();
            if (CollectionUtils.isNotEmpty(orders)) {
                OdataClassMetaData classMetaDataByOdataClass = OdataCachedData.getClassMetaDataByClass(aClass);
                List<String> orderByList = new ArrayList<>();
                for (int i = 0; i < orders.size(); i++) {
                    OrderByItem orderByItem = orders.get(i);
                    Expression expression = orderByItem.getExpression();
                    String objectFiledName = expression.toString();
                    //去除前后的[]
                    objectFiledName = objectFiledName.substring(1, objectFiledName.length() - 1);
                    String dataBaseFiledName = classMetaDataByOdataClass.getObjectFieldToDataBaseFieldMap().get(objectFiledName);

                    if (StringUtils.isNotBlank(dataBaseFiledName)) {
                        String orderBy;
                        boolean isDescending = orderByItem.isDescending();
                        if (isDescending) {
                            orderBy = dataBaseFiledName + SqlSyntaxConstants.SPACE + SqlSyntaxConstants.DESC;
                        } else {
                            orderBy = dataBaseFiledName + SqlSyntaxConstants.SPACE + SqlSyntaxConstants.ASC;
                        }
                        orderByList.add(orderBy);
                    }
                }
                sqlBuild.buildOrderByFlag(true).buildOrderByCriteria(orderByList);
            }

        }else {
            sqlBuild.buildOrderByFlag(false);
        }
    }




    @Override
    public void afterPropertiesSet() throws Exception {
        this.sqlProcessChain.addSqlProcess(this);
    }


}
